package com.wanmait.myhouse.webController;

import com.wanmait.myhouse.dao.FeedBackDAO;
import com.wanmait.myhouse.dao.MediatorDAO;
import com.wanmait.myhouse.dao.UserDAO;
import com.wanmait.myhouse.vo.FeedBack;
import com.wanmait.myhouse.vo.Mediator;
import com.wanmait.myhouse.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Controller
@RequestMapping("web")
public class WebSuggestionController {
    @PostMapping("addFeedBack")
    @ResponseBody
    public String addFeedBack(User user, Mediator mediator, FeedBack feedBack){
        UserDAO userDAO = new UserDAO();
        User userByName = userDAO.userByName(user.getUserName());
        if (!("".equals(mediator.getMediatorName()))){
            MediatorDAO mediatorDAO = new MediatorDAO();
            Mediator mediatorByName = mediatorDAO.mediatorByName(mediator.getMediatorName());
            feedBack.setMediatorId(mediatorByName.getId());
        }
        feedBack.setUserId(userByName.getId());
        feedBack.setDispose(false);
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        Timestamp time = Timestamp.valueOf(localDateTime.format(formatter));
        feedBack.setTime(time);
        FeedBackDAO feedBackDAO = new FeedBackDAO();
        feedBackDAO.addFeedBack(feedBack);
        return "1";
    }

    @GetMapping("suggestion")
    public String suggestion(){
        return "web/suggestion";
    }
}
